

class GameInfo {
    constructor() {
        this.score = 0;                         // 当前得分
        this.historyMaxScore = 0;               // 历史最高分
        this.leftLife = 1;                      // 剩余生命值
        this.maxLife = 2;                       // 最大生命值

        this.maxEnergyNum = 5;                  // 没条生命值最大的能量
        this.leftEnergy = this.maxEnergyNum;    // 当前剩余能力值
		// this.clearPersistendData();

        this.nowMaxValue = 1;                   // 当前数字的最大值
    }
    reset() {
        this.score = 0;
        this.leftLife = 1;
        this.leftEnergy = this.maxEnergyNum;
        this.nowMaxValue = 1;
    };
    addScore(derta) {
        // console.log("addScore() " + derta);
        this.score += derta;
        if (this.score > this.historyMaxScore) {
            this.historyMaxScore = this.score;
        }
    }

    isOver () {
        if (this.leftEnergy <= 0) {
            if (this.leftLife > 0) {
                // 复活
                this.leftLife --;
                this.leftEnergy = this.maxEnergyNum;
                return false;
            } {
                // game over
                this.clearPersistendData();
                return true;
            }
        }
        return false;
    }

    addEnergy() {
        if (this.leftEnergy == this.maxEnergyNum) {
            return;
        }
        this.leftEnergy ++;
    }

    updateNowMaxValue(newValue) {
        if (this.nowMaxValue < newValue) {
            this.nowMaxValue = newValue;
            console.log("nowMaxValue update to " + this.nowMaxValue);
        }
    }

    clearPersistendData() {
        localStorage.removeItem("geziValue");
    }

    persisent2disk(arrGezi) {
        const serializedData = JSON.stringify(this.__serialize());
        localStorage.setItem("gameInfo", serializedData);

        var valueArr = [];
        for(var gezi of arrGezi) {
            valueArr.push(gezi.value);
        }
        localStorage.setItem("geziValue", JSON.stringify(valueArr));
    }

    loadFromDisk() {
        console.log("GameInfo: loadFromDisk()");

        var ret = null;
        var savedData = localStorage.getItem('geziValue');
        if (savedData != null) {
            ret = JSON.parse(savedData);
        }

        savedData = localStorage.getItem('gameInfo');
        if (savedData != null) {
            this.__deserialize(JSON.parse(savedData));
        }

        if (ret == null) {
            this.reset();
        }

        return ret;
    }

    // 自定义持久化方法
    __serialize() {
        return {
            score: this.score,
            historyMaxScore: this.historyMaxScore,
            leftEnergy: this.leftEnergy,
            leftLife: this.leftLife,
            nowMaxValue: this.nowMaxValue,
        };
    }

    // 自定义恢复方法
    __deserialize(data) {
        this.score = data.score;
        this.historyMaxScore = data.historyMaxScore;
        this.leftEnergy = data.leftEnergy;
        this.leftLife = data.leftLife;

        if (this.leftEnergy > this.maxEnergyNum) {
            this.leftEnergy = this.maxEnergyNum;
        }

        this.nowMaxValue = data.nowMaxValue;
    }
};

ScorePanelUi = function(adapterUtils, x, y, w, h, title) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.cornerRadius = adapterUtils.getSize(4);
    this.value = 0;

    this.title = new DrawTextInfo(title, adapterUtils.getSize(20), "#666666");
    this.score = new DrawTextInfo("", adapterUtils.getSize(36), "#fff");

    this.draw = function(ctx) {
        ctx.fillStyle = "#99CC99";
        cavansDrawRoundedRect(ctx, this.x, this.y, this.w, this.h, this.cornerRadius);

        ctx.textAlign = "center";
        ctx.textBaseline = "middle";

        if (this.title.rect == null) {
            var measureTitleH = this.title.textSize;
            var measureScoreH = this.score.textSize;

            var spaceH = (this.h - measureTitleH - measureScoreH) / 3;
            this.title.rect = new Rect(this.x, this.y + spaceH, this.w, measureTitleH);
            this.score.rect = new Rect(this.x, this.y + spaceH*2 + measureTitleH, this.w, measureScoreH);
        }

        // draw title
        ctx.font = "bold " + this.title.textSize + "px Arial";
        ctx.fillStyle = this.title.textColor;
        var text = "" + this.title.text;
        var pos = this.title.getCenterPos();
        ctx.fillText(text, pos.x, pos.y);

        // draw score
        ctx.font = "bold " + this.score.textSize + "px Arial";
        ctx.fillStyle = this.score.textColor;
        text = "" + this.value;
        pos = this.score.getCenterPos();
        ctx.fillText(text, pos.x, pos.y);
    }
}

ImageUI = function(src) {
    this.image = new Image();
    this.image.src = src;
    this.rect = null;
    console.log("new ImageUI(): " + src);

    this.setRect = function(rect) {
        this.rect = rect;
    }

    this.draw = function(ctx, visiable = false) {
        ctx.clearRect(this.rect.x, this.rect.y, this.rect.w, this.rect.h);
        if (visiable) {
            ctx.drawImage(this.image, this.rect.x, this.rect.y, this.rect.w, this.rect.h);
        }
    }
}

class GeziUi {
    constructor(w, h, txtSize) {
        this.w = w;
        this.h = h;
        this.txtSize = txtSize;
    }

    draw(ctx, x, y, value) {
        var cornerRadius = Math.floor(this.w / 10);
        var colorInfo = gColorConfig.getColorByValue(value);
        // 绘制矩形
        ctx.fillStyle = colorInfo.bgColor;
        cavansDrawRoundedRect(ctx, x, y, this.w, this.h, cornerRadius);

        ctx.fill();

        // 绘制文字
        ctx.font = "bold " + this.txtSize + "px Arial";
        ctx.fillStyle = colorInfo.textColor;
        ctx.textAlign = "center";
        ctx.textBaseline = "middle";
        var text = "" + value;
        ctx.fillText(text, x + this.w/2, y + this.h/2);
    }
}

var sLifeImgArr = [];
GameInfoUi = function(adapterUtils, parentW, parentH) {
    this.space = adapterUtils.getSize(60);

    {
        var x = 0; y = adapterUtils.getSize(150);

        this.nowScorePanel = new ScorePanelUi(adapterUtils, x, y, (parentW - this.space)/2, adapterUtils.getSize(90), "Score");

        x += this.nowScorePanel.w + this.space;
        this.historyScorePanel = new ScorePanelUi(adapterUtils, x, y, (parentW - this.space)/2, adapterUtils.getSize(90), "History");

        x=0;

        // 计算红心的位置
        var lifeImgW = adapterUtils.getSize(40);
        y = parentH - lifeImgW - adapterUtils.getSize(30);
        for (var i=0; i<sLifeImgArr.length; i++) {
            sLifeImgArr[i].setRect(new Rect(x, y, lifeImgW, lifeImgW));
            x += lifeImgW + adapterUtils.getSize(1);
        }

        // 计算range ui位置
        var textSize = gAdapterUtils.getSize(20);
        var geziRangeUiW = lifeImgW;
        this.geziRangeUiMax = new GeziUi(geziRangeUiW, geziRangeUiW, textSize);
        this.geziRangeUiMaxX = parentW - geziRangeUiW;
        this.geziRangeUiMaxY = y;
        this.geziRangeUiMin = new GeziUi(geziRangeUiW, geziRangeUiW, textSize);
        this.geziRangeUiMinX = this.geziRangeUiMaxX - geziRangeUiW - adapterUtils.getSize(10);
        this.geziRangeUiMinY = y;
        
    }

    this.draw = function(ctx) {
        this.nowScorePanel.value = gGameInfo.score;
        this.nowScorePanel.draw(ctx);
        this.historyScorePanel.value = gGameInfo.historyMaxScore;
        this.historyScorePanel.draw(ctx);

        // 绘制红心
        for(var i=0; i<gGameInfo.maxLife; i++) {
            sLifeImgArr[i].draw(ctx, i < gGameInfo.leftLife);
        }

        // 绘制当前最大和最小生成数
        var range = gValueControl.getRange();
        this.geziRangeUiMax.draw(ctx, this.geziRangeUiMaxX, this.geziRangeUiMaxY, range.max);
        this.geziRangeUiMin.draw(ctx, this.geziRangeUiMinX, this.geziRangeUiMinY, range.min);
    }
}

GameInfoUi.preload = function(gameInfoObj) {
    for (var i=0; i<gameInfoObj.maxLife; i++) {
        sLifeImgArr.push(new ImageUI("res/img/redHeart.png"));
    }
}